package T01_JDBC的核心API;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 修改 user 用户信息
 */
public class T03_UpdateTest {
    public static void main(String[] args) {
        Connection connection=null;
        Statement  stmt=null;
        try{
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接对象
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3309/test?useSSL=false","root","root");
            //3.定义sql
            String sql="update user set phone='' where name='侯茂昌'";
            //4获取执行sql对象
            stmt=connection.createStatement();
            //5.执行sql
            int count=stmt.executeUpdate(sql);
            //6.处理结果
            System.out.println(count);
            if(count>0){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }
        }catch (ClassNotFoundException e){
             e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //释放资源
            if(stmt!=null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
